<?php

namespace Admin;

use AdminController,
    View,
    Input,
    Jinput,
    Sentry,
    Error,
    Redirect,
    Validate;

class ErrorsController extends AdminController {

    //put your code here
    public function __construct() {
        $this->beforeFilter('auth');
    }

    public function getIndex() {
        $data['appends']['status'] = Jinput::get('status', 'default');
        $data['appends']['type'] = Jinput::get('type', 'default');
        $myModel = new \MyModel();
        $arrParams['left_joins'][] = array('table' => 'users', 'pk' => 'users.id', 'fk' => 'user_id');
        if ($data['appends']['status'] != 'default')
            $arrParams['conditions'][] = array('field' => 'errors.status', 'opr' => '=', 'value' => $data['appends']['status']);
        if ($data['appends']['type'] != 'default') {
            $arrParams['conditions'][] = array('field' => 'errors.type', 'opr' => '=', 'value' => $data['appends']['type']);
        }//$arrParams['paginate']['appends'] = $data['appends'];
        $arrParams['paginate']['nums'] = 25;
        $arrParams['selects'] = array('errors.*', 'users.username');
        $data['errors'] = $myModel->listItems('Error', $arrParams);
        $error = Error::where('id', '=', 8)->get()->first();
        return View::make('backend/errors/index', compact('data', 'error'));
    }

    public function getdelete($id) {
        if (is_null($error = Error::find($id))) {
            return Redirect::to('admin/errors')->with('error', '');
        }
        try {
            $error->delete();
            return Redirect::to('admin/errors');
        } catch (Exception $ex) {
            return Redirect::to('admin/errors', $id)->with('error', $ex);
        }
    }

    public function postEdit() {
        $data = Input::get('data');
        $myModel = new \MyModel();
        $error = $myModel->saveItem('Error', $data['Error'], array('return' => 'class'));
        Return Redirect::back()->with('error_success', 'Thông báo lỗi của bạn đã được gửi đến ban quản trị');
    }

    public function postShowDetail() {
        $id = Input::get('error_id');
        if ($id > 0) {
            $error = Error::where('errors.id', '=', $id)
                            ->leftJoin('users', function($join) {
                                $join->on('users.id', '=', 'errors.user_id');
                            })
                            ->select('errors.*', 'users.username')
                            ->get()->first();
            return \Illuminate\Support\Facades\Response::json(View::make('backend.errors.show.detail_error', array('error' => $error))->render());
        }
    }

}
